package org.example.pojo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
// 核心：通过@JsonInclude注解控制序列化规则
@JsonInclude(JsonInclude.Include.NON_NULL) // 只序列化非null的字段
public class TreeCatalogs
{
	String value;
	String label;
	List<TreeCatalogs> children;
	
	public TreeCatalogs(Catalog c)
	{
		this.value = c.getId();
		this.label = c.getName();
		this.children = null;
	}
	
	//添加到里面 参数一：要找的父亲id，每次递归的顶层集合，要插入的数据
	public static void arrange(String parent_id,List<TreeCatalogs> catalogsList,TreeCatalogs data)
	{
		//找到了
		if(parent_id == null)
		{
			catalogsList.add(data);
			return;
		}
		
		//遍历
		for(TreeCatalogs c: catalogsList)
		{
			//找到父亲
			if(c.getValue().equals(parent_id))
			{
				//如果是空，创建一个集合
				if(c.getChildren() == null || c.getChildren().size() == 0)
				{
					c.setChildren(new ArrayList<>());
					c.getChildren().add(data);
				}
				else {
					c.getChildren().add(data);	
				}
				//找到了要返回出来
				return;
			}
			
			if(c.getChildren() != null)
				arrange(parent_id,c.getChildren(),data);
		}
	}
	
}